package com.weishi.project.training.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.weishi.project.training.domain.TrainingWeek;
import com.weishi.project.training.vo.TrainingDayAddVo;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import java.util.Arrays;
import com.weishi.project.training.domain.TrainingDay;
import com.weishi.project.training.service.ITrainingDayService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.web.page.TableDataInfo;

/**
 * trainingDayController
 */
@Api(value = "训练日项目",description = "训练日项目")
@RestController
@RequestMapping("/training/trainingDay")
public class TrainingDayController extends BaseController {

    @Autowired
    private ITrainingDayService trainingDayService;

    /**
     * 查询训练日项目列表
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingDay trainingDay) {
        startPage();
        List<TrainingDay> list = trainingDayService.selectTrainingDayList(trainingDay);
        return getDataTable(list);
    }

    /**
     * 导出训练日项目列表
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:export')")
    @Log(title = "trainingDay", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TrainingDay trainingDay) {
        List<TrainingDay> list = trainingDayService.selectTrainingDayList(trainingDay);
        ExcelUtil<TrainingDay> util = new ExcelUtil<TrainingDay>(TrainingDay.class);
        return util.exportExcel(list, "trainingDay");
    }

    /**
     * 获取训练日项目基本信息
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:query')")
    @GetMapping("/{id}")
    public AjaxResult getBaseInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingDayService.getTrainingDayById(id));
    }

    /**
     * 获取训练日项目详细信息(航道，小组信息)
     */
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingDayService.getTrainingDayInfoById1(id));
    }

    /**
     * 新增训练日项目
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:add')")
    @Log(title = "trainingDay", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingDay trainingDay) throws ParseException {
        trainingDay.setCtime(new Date());

        // 计算开始时间，和结束时间的差值
        /*分钟差*/
        SimpleDateFormat simpleFormat = new SimpleDateFormat("hh:mm");
        Date fromDate3 = simpleFormat.parse(trainingDay.getStartTime());
        Date toDate3 = simpleFormat.parse(trainingDay.getEndTime());
        long from3 = fromDate3.getTime();
        long to3 = toDate3.getTime();
        int minutes = (int) ((to3 - from3) / (1000 * 60));
        trainingDay.setRealClassHour(minutes);
        trainingDayService.save(trainingDay,true);
        return AjaxResult.success();
    }

    /**
     * 新增训练日项目和训练组
     */
    @Log(title = "trainingDay", businessType = BusinessType.INSERT)
    @PostMapping("/addTrainingDayAndTrainingGroup")
    public AjaxResult addTrainingDayAndTrainingGroup(@RequestBody TrainingDayAddVo trainingDayAddVo) {
        trainingDayService.addTrainingDayAndTrainingGroup(trainingDayAddVo);
        return AjaxResult.success();
    }

    /**
     * 修改训练日项目
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:edit')")
    @Log(title = "trainingDay", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingDay trainingDay) throws ParseException {

        // 计算开始时间，和结束时间的差值
        /*分钟差*/
        SimpleDateFormat simpleFormat = new SimpleDateFormat("hh:mm");
        Date fromDate3 = simpleFormat.parse(trainingDay.getStartTime());
        Date toDate3 = simpleFormat.parse(trainingDay.getEndTime());
        long from3 = fromDate3.getTime();
        long to3 = toDate3.getTime();
        int minutes = (int) ((to3 - from3) / (1000 * 60));
        trainingDay.setRealClassHour(minutes);
        trainingDayService.updateTrainingWeekById(trainingDay);
        return AjaxResult.success();
    }

    /**
     * 删除训练日项目
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:remove')")
    @Log(title = "trainingDay", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        trainingDayService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 查询训练周名称集合
     */
    @PreAuthorize("@ss.hasPermi('training:trainingDay:list')")
    @GetMapping("/getWeekNamelist")
    public TableDataInfo list() {
        startPage();
        List<TrainingWeek> list = trainingDayService.getWeekName();
        return getDataTable(list);
    }
}